/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Acquaintance.IAPlace;
import GoogleMap.DirectionsServiceJSON;
import DALFactory.IDALFactory;
import DALFactory.IDataMapper2;
import DALFactory.IRouteMapper;
import java.io.IOException;
import net.sf.json.JSONObject;

/**
 *
 * @author Administrator
 */
public class CDirection {

    private final static float Lat = 26.051567f;
    private final static float Lng = 119.345316f;
    private DirectionsServiceJSON DS = new DirectionsServiceJSON();
    private IDALFactory factory;
    private IDataMapper2 dataMapper2;
    private IRouteMapper routeMapper;

    public JSONObject getDirection(String ID) throws IOException {
    	factory = CCache.getCache().getDALFactory();
        routeMapper = factory.CreateRouteMapper();
        dataMapper2=factory.CreateDataMapper2();
        if (dataMapper2.exiPlaceByID(ID)) {
            IAPlace destination = dataMapper2.getPlaceByID(ID);
            if (routeMapper.exiRouteByID(ID)) {
                return routeMapper.getRouteByID(ID);
            } else {
                JSONObject json = DS.getDirectionRoute(Lat + "," + Lng, destination.getLocation().getLat() + "," + destination.getLocation().getLng());
                json.accumulate("_id", ID);
                routeMapper.savaeRoute(json);
                return json;
            }
        } else {
            return new JSONObject().accumulate("Status", "Place Not Exist!");
        }
    }
}
